package Twice.day11;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: lya
 * @Description: 本类的作用描述:
 * @CreateDate: 2019/8/6
 * @Version: 1.0
 */
public class ArrayListUpdateDemo {
    public static void main(String[] args) {
        //创建一个用户集合
        List<User> users = new ArrayList<>();

        User u1 = new User(1,"tom");
        User u2 = new User(2,"jack");
        User u3 = new User(3,"success");

        users.add(u1);
        users.add(u2);
        users.add(u3);

        //删除集合中第一次出现的对象 - 提供了hashcode和equals,只要id一样那么就认为是同一对象
        delByUserId(users,2);

    }

    private static void delByUserId(List<User> users,int id) {
        //参数有效性判断
        if(null == users && users.size() == 0){
            return;
        }
        Iterator<User> iter = users.iterator();
        while(iter.hasNext()){
            User u = iter.next();
            if(u.getId().equals(id)){
                iter.remove();
            }
        }
    }
}
